package com.fdb.basic.framework.core.threadpool.config;

import com.fdb.basic.framework.core.threadpool.ApplicationThreadPool2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;

@DisconfFile(filename = "applicationThreadPoolConfig.properties")
@DisconfUpdateService(classes = ApplicationThreadPoolConfig.class)
public class ApplicationThreadPoolConfig implements IDisconfUpdate {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationThreadPoolConfig.class);

    @Value("${applicationThreadPool.maxTaskNum}")
    private Integer maxTaskNum;

    @Autowired
    private ApplicationThreadPool2 threadPool;

    @DisconfFileItem(name = "applicationThreadPool.maxTaskNum", associateField = "maxTaskNum")
    public Integer getMaxTaskNum() {
        return maxTaskNum;
    }

    /**
     * 改变线程池的配置将会暂时停止任务的接收，直到所有等待执行的任务被派发
     *
     * @throws Exception
     */
    @Override
    public void reload() throws Exception {
        logger.info("applicationThreadPoolConfig.properties重新加载！");
        threadPool.change(getMaxTaskNum());
    }


    public void setMaxTaskNum(Integer maxTaskNum) {
        this.maxTaskNum = maxTaskNum;
    }
}
